package com.electricfan.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.electricfan.dao.IBaseDao;

public class BaseDaoImpl extends SqlMapClientDaoSupport implements IBaseDao
{

    public int count(String strSql)
    {
        Integer result = 0;
        try
        {
            result = (Integer) getSqlMapClient().queryForObject(strSql);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    public <T> void batchDelete(final String sqlStr, final List<T> list)
    {
    }

    public <T> void batchInsert(final String sqlStr, final List<T> list)
    {
    }

    public <T> void batchUpdate(final String sqlStr, final List<T> list)
    {
    }

    public <T> int delete(String sqlStr, T primaryKey)
    {
        int rows = -1;
        try
        {
            rows = getSqlMapClient().delete(sqlStr, primaryKey);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return rows;
    }

    @SuppressWarnings("unchecked")
    public <T extends Object, PK> T getOne(String sqlStr, PK primaryKey)
    {
        T reuslt = null;
        try
        {
            reuslt = (T) getSqlMapClient().queryForObject(sqlStr, primaryKey);

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return reuslt;
    }

    @SuppressWarnings("unchecked")
    public <T> T insert(String sqlStr, T entity)
    {
        T result = null;
        try
        {
            result = (T) getSqlMapClient().insert(sqlStr, entity);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> selectAll(String sqlStr)
    {
        List<T> list = new ArrayList<T>();
        try
        {
            list = getSqlMapClient().queryForList(sqlStr);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return list;
    }

    public <T> int update(String sqlStr, T entity)
    {
        int rows = -1;
        try
        {
            rows = getSqlMapClient().update(

            sqlStr, entity);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return rows;
    }

}
